package cn.entity.utils;
import cn.action.CollideAction;
import cn.action.StartAction;
import cn.actionListener.AttackListener;
import cn.entity.img.*;
import cn.entity.master.Boss1;
import cn.entity.master.Fighter;
import cn.entity.master.master;
import cn.entity.master.master1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import static cn.actionListener.AttackListener.pressedKeys;

public class Paint extends JPanel implements ActionListener {
  private Fighter fighter;
  private Background background;
  private int fighterIndex;
  private Timer timer;
  private Frame frame;
  private Boss1 boss1;
  private master1 master2;
  private master1 master3;
  private HpBar hpBar;
  private master master1;
  private CollideAction FighterCollide;
  private CollideAction Master1Collide;
  private CollideAction Master2Collide;
  private CollideAction Master3Collide;
  private CollideAction bigSwordCollide;
  private int masterIndex;
  private int masterIndex1;
  private int masterIndex2;
  private int swordIndex;
  private int flySwordIndex;
  private int FireballIndex;
  public  static int a;
  public static int masterState;
  private Sword sword;
  private Fireball fireball1;
  public static ArrayList<Sword> flySword;
  public static ArrayList<Sword> Sword;
  public static ArrayList<Fireball> fireballs;
  public static ArrayList<bigSword> bigSword;
  private CollideAction SwordCollide;
  private CollideAction FireballCollide;
  private int time;
  private int big1;
  private int big2;
  private Random random;
  private int time1;
  private StartAction startAction;
   public Paint(Fighter fighter,Frame frame,CollideAction collideAction,StartAction startAction) throws IOException {
       random=new Random();
      flySword = new ArrayList<>();
      Sword = new ArrayList<>();
      fireballs = new ArrayList<>();
       bigSword=new ArrayList<>();
      master2=new master1(700,200,50);
      master3=new master1(700,400,50);
      this.startAction=startAction;
      masterState=0;
      a=0;
      time=0;
      time1=0;
      big1=0;
      hpBar= new HpBar();
      master1=new master(0,700,-100,3,300);
      this.frame = frame;
      this.fighter=fighter;
      fighter.setX(50);
      fighter.setY(200);
      fighter.setPower(100);
      background=new Background();
      boss1 =new Boss1();
      timer = new Timer(50, this);
      timer.start();
      this.FighterCollide=collideAction;
      Master1Collide=new CollideAction(master1.getX(),master1.getY(),master1.getWidth(),master1.getHeight());
      Master2Collide=new CollideAction(master2.getX(),master2.getY(),master2.getWidth(),master2.getHeight());
      Master3Collide=new CollideAction(master3.getX(),master3.getY(),master3.getWidth(),master3.getHeight());
      fighter.setHp(50);

  }


    @Override
    public void actionPerformed(ActionEvent e) {
        FighterCollide.setX(fighter.getX());
        FighterCollide.setY(fighter.getY());
        Master1Collide.setX(master1.getX());
        Master1Collide.setY(master1.getY());
        Master2Collide.setX(master2.getX());
        Master2Collide.setY(master2.getY());
        fighterIndex=fighter.getIndex();
        masterIndex=master1.getIndex();
        masterIndex1=master2.getIndex();
        masterIndex2=master3.getIndex();
        master3.setIndex((masterIndex2+1)%master2.getImageLength());
        master2.setIndex((masterIndex1 + 1) %master2.getImageLength());
        master1.setIndex((masterIndex + 1) %master1.getImageLength());
        fighter.setIndex((fighterIndex +1) %fighter.getImageLength() );
        fighter.setImage(fighterIndex);
        master1.setImage(masterIndex);
        master2.setImage(masterIndex1);
        master3.setImage(masterIndex2);
        // 持续蓄力逻辑
        if( AttackListener.pressedKeys.contains(KeyEvent.VK_L)) {
            fighter.chargeBigSword();
        }
        else{
            fighter.resetBigSword();
        }
        repaint();
    }

    public void  paintComponent(Graphics g) {
        super.paintComponent(g);
        background.paint(g);
        hpBar.paint(g,fighter.geti());

        fighter.paintComponent(g);
        switch (a){
            case 0:
                master1.paint(g);
                master1.addWith();
                if(master1.master1Come()){
                    try {
                        Thread.sleep(300);
                        a=1;
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                };
                break;
            case 1:
                master1.paint(g);
                fighter.step();
                master1.master1Step();
                if(background.getStop()==0)   {
                    boss1.paint(g);
                    fighter.setState(boss1.step());
                }

                if(FighterCollide.intersects(Master1Collide)){
                    hpBar.setHpBarWidth(2,fighter.getLevel());
                    fighter.getPower(1);
                }
                int yMax=fighter.getY()+fighter.getHeight()/2;
                int yMin=fighter.getY()-fighter.getHeight()/2;
                int YMax=master1.getY()+master1.getHeight()/2;
                int YMin=master1.getY()-master1.getHeight()/2;

                if(yMax>YMin&&yMax<YMax || yMin>YMin&&yMin<YMax &&master1.getHitWidth()>300){
                    time+=5;
                    if(time==120){
                        try {
                            fireballs.add(new Fireball(master1.getX(),master1.getY(),0,0));
                            time=0;
                            big1++;
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
//                if(master1.getHitWidth()<300){
//                    try{
//                        time+=5;
//                        if(time>=100&&big2<10){
//                            int  randomNumber1 = random.nextInt(350) + 1;
//                            fireballs.add(new Fireball(550,randomNumber1));
//                            big1+=1;
//                            time=0;
//                        }
//
//                        else if (big1>=8){
//                            for(int i=0;i<3;i++){
//                                int  randomNumber = random.nextInt(350) + 1;
//                                fireballs.add(new Fireball(550,randomNumber));
//                            }
//                            big1-=5;
//                            big2+=3;
//
//                        }
//                        else if(big2>=10){
//                            master1.setY(0);
//                            masterState=1;
//                            big2=0;
//                        }
//                    }catch (Exception e){
//                        throw new RuntimeException(e);
//                    }
//                }
                if(master1.getHitWidth()<450){
                    master1.setY(-100);
                    a=3;
                }
                FireballUpdate();
                break;
            case 2:

                fighter.step();
                if(master2.getHitWidth()>=0){
                    master2.paint(g);

                }
                if(master3.getHitWidth()>=0){
                    master3.paint(g);
                }
               if(master2.getHitWidth()<=0){
                    master2.setY(-100);
                }
               if(master3.getHitWidth()<=0){
                    master3.setY(-100);
                }

                if(FighterCollide.intersects(Master2Collide)){
                    hpBar.setHpBarWidth(6,fighter.getLevel());
                    fighter.getPower(1);
                }

                time += 5;
                if (time >= 350 && !master2.isMoving()) {
                    master2.startMovingToPlayer(fighter.getX(), fighter.getY());
                    time = 0;
                }
                time1+=5;
                if(time1>=240 && master3.getHitWidth()>0){
                    if(fireballs.size()>3){
                        time1=0;
                    }

                    else if(master3.getHitWidth()>0){
                        try {
                            fireballs.add(new Fireball(master3.getX(),master3.getY(),fighter.getY(),fighter.getX()));
                            time1=0;
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
                FireballUpdate(fighter.getX(),fighter.getY());
                break;
            case 3:

                if(!fireballs.isEmpty()){
                    fireballs.clear();
                }

                fighter.step();
                int b=background.step(10);
                if(b==1){
                    time=0;
                    a=2;
                }
                else if(b==2){
                    a=4;
                }
                break;

        }

        for(Sword sword:Sword){
            sword.setX(fighter.getX());
            sword.setY(fighter.getY());
            SwordCollide=new CollideAction(sword.getX(),sword.getY(),sword.getWidth(),sword.getHeight());
            sword.paintComponent(g);
            swordIndex=sword.getIndex();
            sword.setIndex((swordIndex+1)%sword.getImage1Length());
            sword.setImage(swordIndex);
            if(Master1Collide.intersects(SwordCollide)){
                master1.setHp(1,fighter.getLevel());
                fighter.getPowerWhitSword(3);
            }
            else if(Master2Collide.intersects(SwordCollide)&&a==2){
                master2.setHp(1,fighter.getLevel());
                fighter.getPowerWhitSword(3);
            }
            else if(Master3Collide.intersects(SwordCollide)&&a==2){
                master3.setHp(1,fighter.getLevel());
                fighter.getPowerWhitSword(3);
            }
        }
        for(Fireball fireball:fireballs){
            fireball.paintComponent(g);
            FireballIndex=fireball.getIndex();
            fireball.setIndex((FireballIndex+1)%fireball.getImageLength());
            fireball.setImage(FireballIndex);
            FireballCollide=new CollideAction(fireball.getX(),fireball.getY(),100,45);
            if(FighterCollide.intersects(FireballCollide)) {
                hpBar.setHpBarWidth(30,fighter.getLevel());
                fighter.getPower(10);
                fireball.setX(-99);
            }
        }
        for(Sword sword:flySword){
            sword.paint(g);
            flySwordIndex=sword.getIndex1();
            SwordCollide=new CollideAction(sword.getFlySwordX(),sword.getFlySwordY(),sword.getWidth(),sword.getHeight());
            sword.setIndex1((flySwordIndex+1)%sword.getImage1Length());
            sword.setImage1(flySwordIndex);
            if(Master1Collide.intersects(SwordCollide)){
                master1.setHp(2,fighter.getLevel());
            }
            else if(Master2Collide.intersects(SwordCollide)&&a==2){
                master2.setHp(2,fighter.getLevel());
            }
            else if(Master3Collide.intersects(SwordCollide)&&a==2){
                master3.setHp(2,fighter.getLevel());
            }
        }
        for(int i = 0; i < bigSword.size(); i++) {
            bigSword bs = bigSword.get(i);
            // 设置攻击动画位置与fighter一致
            bs.setAttackPosition(fighter.getX(), fighter.getY());
            bigSwordCollide=new CollideAction(bs.getX(),bs.getY(),bs.getWidth(),bs.getHeight());
            if(Master1Collide.intersects(bigSwordCollide)){
                master1.setHp(3,fighter.getLevel());

            }
            if(Master2Collide.intersects(bigSwordCollide)&&a==2){
                master2.setHp(3,fighter.getLevel());
            }
            if(Master3Collide.intersects(bigSwordCollide)&&a==2){
                master3.setHp(3,fighter.getLevel());
            }

            if(bs.isAnimationFinished()) {
                bigSword.remove(i);
                i--; // 调整索引
                continue; // 跳过绘制
            }

            bs.paintComponent(g);
        }
        flySwordUpdate();
        fighter.setHp(hpBar.getHpBarWidth());
        stop();
     }
    public void stop(){
       if(fighter.getHp()<=0){
           timer.stop();
           startAction.reBack();
       }
      else if(master2.getHitWidth()<=0&&master3.getHitWidth()<=0){
         timer.stop();
         startAction.reBack();
      }
    }


    public void flySwordUpdate(){
        for (int i = 0; i < flySword.size(); i++) {
            Sword fly = flySword.get(i);
            fly.flySwordStep();
            if (fly.getX() > 750) {
                flySword.remove(i);
                i--;
            }
        }
    }

    public void FireballUpdate(){
        for (int i = 0; i < fireballs.size(); i++) {
            Fireball fly = fireballs.get(i);
            fly.step();
            if (fly.getX()<-100) {
                fireballs.remove(i);
                i--;
            }
        }
    }
    public void FireballUpdate(int x,int y){
        for (int i = 0; i < fireballs.size(); i++) {
            Fireball fly = fireballs.get(i);
            fly.step1();
            if (fly.getX()<-100) {
                fireballs.remove(i);
                i--;
            }
        }
    }
    }

